
******************************************************************************************
**** This file reads the original data from CIS, performs some transformations of the 
**** original data and generates a STATA dta file
*****************************************************************************************

clear 
clear matrix
set memory 100000K
log using ElectoralData.log, replace
infix A1 1-9  CUES 5-9  CCAA 10-11  PROV 12-13  MUN 14-16  TAMUNI 17-17  AREA 18-18 DISTR 19-20  /*
*/ SECCION 21-23  ENTREV 24-27  MUESTRA 28-28  P1 29-29  P201 30-30 P202 31-31  P203 32-32  P204 33-33 /*
*/ P301 34-35  P302 36-37  P401 38-38  P402 39-39 P403 40-40  P501 41-41  P502 42-42  P503 43-43  /*
*/ P504 44-44  P505 45-45  P601 46-47 P602 48-49  P7 50-51  P7A 52-52  P8 53-54  P8A 55-55  P9 56-57  /*
*/ P9A 58-58 P10 59-60  P10A 61-61  P11 62-63  P11A 64-64  P12 65-65  P12A 66-66  P13 67-67 P1401 68-68  /*
*/ P1402 69-69  P1403 70-70  P1404 71-71  P1501 72-72  P1502 73-73 P1503 74-74  P1504 75-75  P1505 76-76 /* 
*/ P1506 77-77  P1507 78-78  P1508 79-79 P1509 80-80  P1510 81-81  P1511 82-82  P1512 83-83 P1513 84-84 /*
*/ P15A 85-86 P15B 87-88  P15C 89-89  P16 90-90  P17 91-91  P18 92-92  P19 93-93  P2001 94-94 P2002 95-95  /*
*/ P2003 96-96  P2004 97-97  P21 98-99  P21A 100-101  P2201 102-102 P2202 103-103  P2203 104-104  /*
*/ P22A 105-106  P22B 107-108  P22C 109-110 P23 111-111  P23A 112-112  P23B01 113-113  P23B02 114-114  /*
*/ P23B03 115-115 P23B04 116-116  P23B05 117-117  P23B06 118-118  P24 119-119  P24A 120-120 P24B 121-121 /*
*/ P2501 122-122  P2502 123-123  P2503 124-124  P2504 125-125 P2505 126-126  P26 127-127  P26A01 128-128 /*
*/ P26A02 129-129  P26A03 130-130 P26A04 131-131  P26A05 132-132  P26A06 133-133  P26A07 134-134  /*
*/ P26A08 135-135 P26A09 136-136  P26A10 137-137  P26A11 138-138  P26A12 139-139  P26A13 140-140 /*
*/ P26A14 141-141  P2701 155-155  P2702 156-156  P2703 157-157  P2704 158-158 P2801 159-159  P2802 160-160 /*
*/ P28A01 161-161  P28A02 162-162  P2901 163-163 P2902 164-164  P2903 165-165  P2904 166-166  P2905 167-167 /*
*/ P2906 168-168 P2907 169-169  P2908 170-170  P2909 171-171  P2910 172-172  P2911 173-173 P2912 174-174  /*
*/ P30 175-175  P30A 176-176  P30B 177-177  P31 178-178 P31A01 179-180  P31A02 181-182  P32 183-183  /*
*/ P32A 184-184  P32B01 185-186 P32B02 187-188  P32C 189-190  P33 191-191  P34 192-193  P35 194-194  /*
*/ P36A 195-195 P36B 196-196  P36C 197-197  P37 198-198  P38 199-199  P39 200-200  P40 201-201 P41 202-203 /*
*/ P4201 204-205  P4202 206-207  P4203 208-209  P4301 210-211 P4302 212-213  P4303 214-215  P4304 216-217  /*
*/ P4305 218-219  P4306 220-221 P4307 222-223  P4308 224-225  P4309 226-227  P4310 228-229  P4311 230-231 /*
*/ P4312 232-233  P44 234-234  P44A 235-236  P44B 237-237  P45 238-238  P46 239-240 P4701 241-242  /*
*/ P4702 243-244  P4703 245-246  P4704 247-248  P4705 249-250 P4706 251-252  P4707 253-254  P4708 255-256 /*
*/ P4709 257-258  P4710 259-260 P4711 261-262  P48 263-263  P49 264-264  P50 265-268  P51 269-269  /*
*/ P5201 270-270 P5202 271-271  P5203 272-272  P5204 273-273  P53 274-274  P53A01 275-275 P53A02 276-276 /*
*/ P54 277-277  P54A 278-278  P54B01 279-280  P54B02 281-282 P54C 283-283  P55 284-284  P56 285-285  /*
*/ P57 286-287  P5801 288-288  P5802 289-289 P5803 290-290  P58A 291-291  P59 292-292  P60 293-293  /*
*/ P61 294-294  P61A 295-296 P62 297-297  P62A 298-298  P63 299-299  P64 300-300  P65 301-303  P66 304-304 /*
*/ P66A 305-305  P67 306-307  P68 308-309  P69 310-310  P70 311-311  P70A 312-315 P70B 316-317  /*
*/ COL319 319-319  SEXO 323-323  EDAD 324-325  P26A0101 336-336 P26A0102 337-337  P26A0103 338-338  /*
*/ P26A0104 339-339  P26A0105 340-340 P26A0106 341-341  P26A0107 342-342  P26A0201 343-343  P26A0202 344-344 /*
*/ P26A0203 345-345  P26A0204 346-346  P26A0205 347-347  P26A0206 348-348 P26A0207 349-349  P26A0301 350-350  /*
*/ P26A0302 351-351  P26A0303 352-352 P26A0304 353-353  P26A0305 354-354  P26A0306 355-355  P26A0307 356-356 /*
*/ P26A0401 357-357  P26A0402 358-358  P26A0403 359-359  P26A0404 360-360 P26A0405 361-361  P26A0406 362-362  /*
*/ P26A0407 363-363  P26A0501 364-364 P26A0502 365-365  P26A0503 366-366  P26A0504 367-367  P26A0505 368-368 /*
*/ P26A0506 369-369  P26A0507 370-370  P26A0601 371-371  P26A0602 372-372 P26A0603 373-373  P26A0604 374-374  /*
*/ P26A0605 375-375  P26A0606 376-376 P26A0607 377-377  P26A0701 378-378  P26A0702 379-379  P26A0703 380-380 /*
*/ P26A0704 381-381  P26A0705 382-382  P26A0706 383-383  P26A0707 384-384 P26A0801 385-385  P26A0802 386-386 /*
*/ P26A0803 387-387  P26A0804 388-388 P26A0805 389-389  P26A0806 390-390  P26A0807 391-391  P26A0901 392-392 /*
*/ P26A0902 393-393  P26A0903 394-394  P26A0904 395-395  P26A0905 396-396 P26A0906 397-397  P26A0907 398-398 /*
*/ P26A1001 399-399  P26A1002 400-400 P26A1003 401-401  P26A1004 402-402  P26A1005 403-403  P26A1006 404-404 /*
*/ P26A1007 405-405  P26A1101 406-406  P26A1102 407-407  P26A1103 408-408 P26A1104 409-409  P26A1105 410-410 /*
*/ P26A1106 411-411  P26A1107 412-412 P26A1201 413-413  P26A1202 414-414  P26A1203 415-415  P26A1204 416-416 /* 
*/ P26A1205 417-417  P26A1206 418-418  P26A1207 419-419  P26A1301 420-420 P26A1302 421-421  P26A1303 422-422 /*
*/ P26A1304 423-423  P26A1305 424-424 P26A1306 425-425  P26A1307 426-426  str PESO 427-431 using "DA2757.txt"
 
****************************
** Variable transformations 
****************************
 
** INCOME **
rename P68 ingresos
**** We group income in 4 categories: ingresos1=(less than 900 euros), ingresos2=(901-1800 euros), 
**** ingresos3=(1801-3000 euros), ingresos4=(more than 3000 euros)
gen ingresos1=ingresos==1| ingresos==2|ingresos==3
gen ingresos2=ingresos==4|ingresos==5
gen ingresos3=ingresos==6|ingresos==7
gen ingresos4=ingresos==8|ingresos==9|ingresos==10
***drop those individuals who do not report income
drop if ingresos==99

rename  P69 clasesocial

**** we build three SOCIAL CLASSES: clasesocial1=(high, medium-high), clasesocial2=(medium), 
**** clasesocial3=(medium low, low)
gen clasesocial1=clasesocial==1|clasesocial==2
gen clasesocial2=clasesocial==3
gen clasesocial3=clasesocial==4|clasesocial==5

rename  P64 situlaboral

**** there are four possible LABORAL SITUATIONS: situlaboral1=(work), situlaboral2=(pensioner or retired), 
**** situlaboral3=(unemployed or unpaid homework), situlaboral4=(student)
gen situlaboral1=situlaboral==1
gen situlaboral2=situlaboral==2|situlaboral==3
gen situlaboral3=situlaboral==4|situlaboral==5
gen situlaboral4=situlaboral==6

rename  P66 tipotrabajo

*** RELIGION
rename  P62 religion
**** religion: religion1=(catholic), religion2=(believer of another religion), 
**** religion3=not a believer / atheist / does not respond
gen religion1=religion==1
gen religion2=religion==2
gen religion3=religion==3|religion==4|religion==9
rename  P62A praticareligion
*** 1 if individual attends church or cult at least once a month, otherwise 0.
gen praticareligion1=(religion==1 & praticareligion==3)|(religion==1 & praticareligion==4)| /*
*/ (religion==1 & praticareligion==5)|(religion==2 & praticareligion==3)|(religion==2 & praticareligion==4)|(religion==2 & praticareligion==5)
***drop if the individual does not answer about the religion
drop if religion==9
drop if praticareligion==9

*** STUDIES
rename  P61 estu 
rename  P61A nivelestu
***level of studies: estudios1=no studies; estudios2= compulsory education; 
***estudios3= (medium professional studies "grado medio", high school, high professional studies "grado superior"); 
***estudios4= university studies; estudios5= graduate studies
gen estudios=0
replace estudios=5 if estu==3 & nivelestu==1
replace estudios=12 if estu==3 & nivelestu==2
replace estudios=16 if estu==3 & nivelestu==3
replace estudios=18 if estu==3 & nivelestu==4
replace estudios=18 if estu==3 & nivelestu==5
replace estudios=20 if estu==3 & nivelestu==6
replace estudios=21 if estu==3 & nivelestu==7
replace estudios=21 if estu==3 & nivelestu==8
replace estudios=23 if estu==3 & nivelestu==9
replace estudios=22 if estu==3 & nivelestu==10
replace estudios=26 if estu==3 & nivelestu==11
drop if estu==9
gen estudios1=0
replace estudios1=1 if estu==1|estu==2
gen estudios2=0
replace estudios2=1 if estu==3 & nivelestu==1
replace estudios2=1 if estu==3 & nivelestu==2
replace estudios2=1 if estu==3 & nivelestu==3
gen estudios3=0
replace estudios3=1 if estu==3 & nivelestu==4
replace estudios3=1 if estu==3 & nivelestu==5
replace estudios3=1 if estu==3 & nivelestu==6
gen estudios4=0
replace estudios4=1 if estu==3 & nivelestu==7
replace estudios4=1 if estu==3 & nivelestu==8
replace estudios4=1 if estu==3 & nivelestu==9
replace estudios4=1 if estu==3 & nivelestu==10
gen estudios5=(estu==3 & nivelestu==11)

*** MARITAL STATUS
rename  P59 Estadocivil
gen Casado=Estadocivil==1
gen soltero=Estadocivil==2
gen viudo=Estadocivil==3
gen separadodivorciado=Estadocivil==4|Estadocivil==5

*** PREVIOUS GENERAL ELECTIONS
rename  P57 partido2004
replace partido2004=13 if partido2004==97
gen partido2004PSOE=partido2004==1
gen partido2004PP=partido2004==2
gen partido2004IU=partido2004==3
gen partido2004CIU=partido2004==4
gen partido2004ERC=partido2004==5
gen partido2004PNV=partido2004==6
gen partido2004BNG=partido2004==7
gen partido2004CC=partido2004==8
gen partido2004EA=partido2004==9
gen partido2004CHA=partido2004==10
gen partido2004NaBai=partido2004==11
gen partido2004abstain=partido2004==13

*** LEVEL OF NATIONALISM
rename  P46 Naciona 
rename  P4701 NacioPsoe
rename  P4702 NacioPP
rename  P4703 NacioIU
rename  P4704 NacioUpyd
rename  P4705 NacioCiu
rename  P4706 NacioErc
rename  P4707 NacioPnv
rename  P4708 NacioEa
rename  P4709 NacioBng
rename  P4710 NacioCc
rename  P4711 NacioNabai

***CLOSEST PARTY
rename P44 partidoproximoSI
rename P44A partidoproximo


*** LEFT-RIGHT
rename  P41 IzqDer
***drop if no answer
drop if IzqDer==98|IzqDer==99


**The vote of those who vote
rename  P34 voto
***those who abstain
replace voto=13 if voto==0
**does she vote or not?
rename  P32 votarnovotar
***a variable that takes value 1 if she votes and 0 otherwise
gen votarnovotar1=votarnovotar==4
**why did you vote for Psoe, Pp o Iu?
rename  P36A votoPsoe
rename  P36B votoPp
rename  P36C votoIu

*** drop the individuals who do not manifest what they vote
***those who do not say whether they voted or not
drop if votarnovotar==9
***those who do not reveal their vote 
drop if voto==98|voto==99   
***those who vote for a party that did not obtain a seat
drop if voto==49|voto==96

**drop those who did not vote because they were not registed in the census
drop if P32B01==8

***identify sincere abstainers
gen abstainseguro=0
replace abstainseguro=1 if voto==13 & P32B01==1
replace abstainseguro=1 if voto==13 & P32B01==2
replace abstainseguro=1 if voto==13 & P32B01==3
replace abstainseguro=1 if voto==13 & P32B01==5
replace abstainseguro=1 if voto==13 & P32B01==6

**Drop the abstainers that we are not sure that they voted sincerely
drop if abstainseguro==0 & voto==13

**strategic vote: those who voted to avoid the other party to win (option 6)
gen votoestrategico=votoPsoe==6|votoPp==6

*****knowledge about the surveys****
rename  P30 encuestas
gen conoencuestas=encuestas==1

****knowledge about the debate****
rename  P24 debates
gen conodebates=debates==1|debates==2|debates==3

**** ECONOMIC SITUATION***
rename P17 situaeconomica
***1 if individual's evaluation is very good, good and 0 otherwise
gen evalueconomica=situaeconomica==1|situaeconomica==2|situaeconomica==3
***drop if economic evalution is not reported
drop if situaeconomica==8|situaeconomica==9

****POLITICAL SITUATION
rename  P16 situapolitica
*** 1 if individual's evaluation is very good, good or regular and 0 otherwise
gen evalupolitica=situapolitica==1|situapolitica==2|situapolitica==3
***drop if political evalution is not reported
drop if situapolitica==8|situapolitica==9

**** PARTY AFFILIATION
rename P1503 afiliacpartido
*** 1 if party member, 0 otherwise 
gen siafiliacpartido=afiliacpartido==1
*** drop if party affiliation is not reported
drop if afiliacpartido==9


*** UNION AFFILIATION
rename  P1504 afiliacsindicato 
***is s/he a member of a uniom?****
gen siafiliacsindicato=afiliacsindicato==1
***drop if s/he does not say anything about union affiliation
drop if afiliacsindicato==9


***CLOSEST PARTY: the variable to see which the closest party is in the most discussed topic in the campaign
rename P21A cercania
replace cercania=13 if cercania==97
*drop those who do not answer this question
drop if cercania==98|cercania==99|cercania==0

***INFORMATION ABOUT PARTIES: the variable to measure the number of ways that parties use to inform during the campaign
gen InfPSOE=P26A01==1
gen InfPP=P26A02==1
gen InfIU=P26A03==1
gen InfUPyD=P26A04==1
gen InfCIU=P26A05==1
gen InfERC=P26A06==1
gen InfPNV=P26A07==1
gen InfBNG=P26A08==1
gen InfCC=P26A09==1
gen InfEA=P26A10==1
gen InfCHA=P26A11==1
gen InfNaBai=P26A12==1

***inform PSOE
gen PSOEcorreo=P26A0101==1
gen PSOEemail=P26A0102==2
gen PSOEsms=P26A0103==3
gen PSOEtelef=P26A0104==4
gen PSOEperso=P26A0105==5
gen PSOEotro=P26A0106==6

***inform PP
gen PPcorreo=P26A0201==1
gen PPemail=P26A0202==2
gen PPsms=P26A0203==3
gen PPtelef=P26A0204==4
gen PPperso=P26A0205==5
gen PPotro=P26A0206==6

***inform IU
gen IUcorreo=P26A0301==1
gen IUemail=P26A0302==2
gen IUsms=P26A0303==3
gen IUtelef=P26A0304==4
gen IUperso=P26A0305==5
gen IUotro=P26A0306==6

***infor UPyD
gen UPyDcorreo=P26A0401==1
gen UPyDemail=P26A0402==2
gen UPyDsms=P26A0403==3
gen UPyDtelef=P26A0404==4
gen UPyDperso=P26A0405==5
gen UPyDotro=P26A0406==6

***indorm CIU
gen CIUcorreo=P26A0501==1
gen CIUemail=P26A0502==2
gen CIUsms=P26A0503==3
gen CIUtelef=P26A0504==4
gen CIUperso=P26A0505==5
gen CIUotro=P26A0506==6

***inform ERC
gen ERCcorreo=P26A0601==1
gen ERCemail=P26A0602==2
gen ERCsms=P26A0603==3
gen ERCtelef=P26A0604==4
gen ERCperso=P26A0605==5
gen ERCotro=P26A0606==6

***inform PNV
gen PNVcorreo=P26A0701==1
gen PNVemail=P26A0702==2
gen PNVsms=P26A0703==3
gen PNVtelef=P26A0704==4
gen PNVperso=P26A0705==5
gen PNVotro=P26A0706==6

***inform BNG
gen BNGcorreo=P26A0801==1
gen BNGemail=P26A0802==2
gen BNGsms=P26A0803==3
gen BNGtelef=P26A0804==4
gen BNGperso=P26A0805==5
gen BNGotro=P26A0806==6

***inform CC
gen CCcorreo=P26A0901==1
gen CCemail=P26A0902==2
gen CCsms=P26A0903==3
gen CCtelef=P26A0904==4
gen CCperso=P26A0905==5
gen CCotro=P26A0906==6

***informa EA
gen EAcorreo=P26A1001==1
gen EAemail=P26A1002==2
gen EAsms=P26A1003==3
gen EAtelef=P26A1004==4
gen EAperso=P26A1005==5
gen EAotro=P26A1006==6

***inform CHA
gen CHAcorreo=P26A1101==1
gen CHAemail=P26A1102==2
gen CHAsms=P26A1103==3
gen CHAtelef=P26A1104==4
gen CHAperso=P26A1105==5
gen CHAotro=P26A1106==6

***inform NaBai
gen NaBaicorreo=P26A1201==1
gen NaBaiemail=P26A1202==2
gen NaBaisms=P26A1203==3
gen NaBaitelef=P26A1204==4
gen NaBaiperso=P26A1205==5
gen NaBaiotro=P26A1206==6

***How many ways to inform has each party?
gen PSOEInf=0
replace PSOEInf=PSOEcorreo+PSOEemail+PSOEsms+PSOEtelef+PSOEperso+PSOEotro if InfPSOE==1
replace PSOEInf=1 if InfPSOE==1 & P26A0107==9

gen PPInf=0
replace PPInf=PPcorreo+PPemail+PPsms+PSOEtelef+PPperso+PPotro if InfPP==1
replace PPInf=1 if InfPP==1 & P26A0207==9

gen IUInf=0
replace IUInf=IUcorreo+IUemail+IUsms+IUtelef+IUperso+IUotro if InfIU==1
replace IUInf=1 if InfIU==1 & P26A0307==9

gen UPyDInf=0
replace UPyDInf=UPyDcorreo+UPyDemail+UPyDsms+UPyDtelef+UPyDperso+UPyDotro if InfUPyD==1
replace UPyDInf=1 if InfUPyD==1 & P26A0407==9

gen CIUInf=0
replace CIUInf=CIUcorreo+CIUemail+CIUsms+CIUtelef+CIUperso+CIUotro if InfCIU==1
replace CIUInf=1 if InfCIU==1 & P26A0507==9

gen ERCInf=0
replace ERCInf=ERCcorreo+ERCemail+ERCsms+ERCtelef+ERCperso+ERCotro if InfERC==1
replace ERCInf=1 if InfERC==1 & P26A0607==9

gen PNVInf=0
replace PNVInf=PNVcorreo+PNVemail+PNVsms+PNVtelef+PNVperso+PNVotro if InfPNV==1
replace PNVInf=1 if InfPNV==1 & P26A0707==9

gen BNGInf=0
replace BNGInf=BNGcorreo+BNGemail+BNGsms+BNGtelef+BNGperso+BNGotro if InfBNG==1
replace BNGInf=1 if InfBNG==1 & P26A0807==9

gen CCInf=0
replace CCInf=CCcorreo+CCemail+CCsms+CCtelef+CCperso+CCotro if InfCC==1
replace CCInf=1 if InfCC==1 & P26A0907==9

gen EAInf=0
replace EAInf=EAcorreo+EAemail+EAsms+EAtelef+EAperso+EAotro if InfEA==1
replace EAInf=1 if InfEA==1 & P26A1007==9

gen CHAInf=0
replace CHAInf=CHAcorreo+CHAemail+CHAsms+CHAtelef+CHAperso+CHAotro if InfCHA==1
replace CHAInf=1 if InfCHA==1 & P26A1107==9

gen NaBaiInf=0
replace NaBaiInf=NaBaicorreo+NaBaiemail+NaBaisms+NaBaitelef+NaBaiperso+NaBaiotro if InfNaBai==1
replace NaBaiInf=1 if InfNaBai==1 & P26A1207==9

***GENDER
gen sexo=SEXO==2

*** DROP THE VARIABLES THAT WE ARE NOT GOING TO USE
drop  P1 P201 P202 P203 P204 P301 P302 P401 P402 P403 P501 P502 P503 P504 P505 P601 P602 P7 P7A P8 P8A P9 P9A P10 P10A P11 P11A P12 P12A P13 P1401 P1402 P1403 P1404 P1501 P1502 P1505 P1506 P1507 P1508 P1509 P1510 P1511 P1512 P1513 P15A P15B P15C P18 P19 P2001 P2001 P2003 P2004 P2201 P2202 P2203 P22A P22B P22C P23 P23A P23B01 P23B02 P23B03 P23B04 P23B05 P23B06 P24A P24B P2501 P2502 P2503 P2504 P2505 P26 P26A01 P26A02 P26A03 P26A04 P26A05 P26A06 P26A07 P26A08 P26A09 P26A10 P26A11 P26A12 P26A13 P26A14 P2701 P2702 P2703 P2704 P2801 P2802 P28A01 P28A02 P2901 P2902 P2903 P2904 P2905 P2906 P2907 P2908 P2909 P2910 P2911 P2912 P30A P30B P31 P31A01 P31A02 P32A P32C P33 P35 P37 P38 P39 P40 P4201 P4202 P4203 P4301 P4302 P4303 P4304 P4305 P4306 P4307 P4308 P4309 P4310 P4311 P4311 P44B P45 P48 P49 P50 P51 P5201 P5202 P5203 P5204 P53 P53A01  P53A02 P54 P54A P54B01 P54B02 P54C P55 P56 P5801 P5802 P5803 P58A P60 P63 P65 P67 P70 P70A P70B P26A0101 P26A0102 P26A0103 P26A0104 P26A0105 P26A0106 P26A0107 P26A0201 P26A0202 P26A0203 P26A0204 P26A0205 P26A0207 P26A0301 P26A0302 P26A0303 P26A0304 P26A0305 P26A0306 P26A0307 P26A0401 P26A0402 P26A0403 P26A0404 P26A0405 P26A0406 P26A0407 P26A0501 P26A0502 P26A0503 P26A0504 P26A0505 P26A0506 P26A0507 P26A0601 P26A0602 P26A0603 P26A0604 P26A0605 P26A0606 P26A0607 P26A0701 P26A0702 P26A0703 P26A0704 P26A0705 P26A0706 P26A0707 P26A0801 P26A0802 P26A0803 P26A0804 P26A0805 P26A0806 P26A0807 P26A0901 P26A0902 P26A0903 P26A0904 P26A0905 P26A0906 P26A0907 P26A1001 P26A1002 P26A1003 P26A1004 P26A1005 P26A1006 P26A1007 P26A1101 P26A1102 P26A1103 P26A1104 P26A1105 P26A1106 P26A1107 P26A1201 P26A1202 P26A1203 P26A1204 P26A1205 P26A1206 P26A1207 P26A1301 P26A1302 P26A1303 P26A1304 P26A1305 P26A1306 P26A1307

***generate a variable that takes value 1 if there is a nationalist party in that constituency, and 0 otherwise
gen partinacio=0
replace partinacio=1 if CCAA==2|CCAA==9|CCAA==12|CCAA==15|CCAA==16


***build an indicator for each individual
gen id=_n
***as there are 13 posible votes, we build 13 replica for each individual
expand 13
**sort the observations by id 
sort id
***generate the alternatives for each individual
by id: gen alter=_n
***build the variable choice that takes value 1 for the alternative chosen by the individual
gen choice=alter==voto


*** THE CLOSEST PARTY: The variable that measures the which the closest party with respect to the most important topic during the campaign
gen ParCercano=0
replace ParCercano=1 if alter==cercania


***INFORMATION ABOUT THE PARTIES: number of ways by which each party reached each individual
gen informacion=0
replace informacion=PSOEInf if alter==1
replace informacion=PPInf if alter==2
replace informacion=IUInf if alter==3
replace informacion=UPyDInf if alter==4
replace informacion=CIUInf if alter==5
replace informacion=ERCInf if alter==6
replace informacion=PNVInf if alter==7
replace informacion=BNGInf if alter==8
replace informacion=CCInf if alter==9
replace informacion=EAInf if alter==10
replace informacion=CHAInf if alter==11
replace informacion=NaBaiInf if alter==12

*** PREVIOUS ELECTIONS: vote in the previous general election
gen votoanterior=0
replace votoanterior=1 if alter==1 & partido2004==1 
replace votoanterior=1 if alter==2 & partido2004==2
replace votoanterior=1 if alter==3 & partido2004==3
replace votoanterior=1 if alter==5 & partido2004==4
replace votoanterior=1 if alter==6 & partido2004==5
replace votoanterior=1 if alter==7 & partido2004==6
replace votoanterior=1 if alter==8 & partido2004==7
replace votoanterior=1 if alter==9 & partido2004==8
replace votoanterior=1 if alter==10 & partido2004==9
replace votoanterior=1 if alter==11 & partido2004==10
replace votoanterior=1 if alter==12 & partido2004==11
replace votoanterior=1 if alter==13 & partido2004==13

*****DROP THE VOTERS OF NaBai, CHA, EA (and also these parties as alternatives)
drop if voto==12
drop if voto==11
drop if voto==10
drop if alter==12 
drop if alter==11
drop if alter==10


**Restrict the model to the possible alternatives of each individual**

**Drop the alternatives that are not available for each individual
drop if alter==5 & NacioCiu==0
drop if alter==6 & NacioErc==0
drop if alter==7 & NacioPnv==0
drop if alter==8 & NacioBng==0
drop if alter==9 & NacioCc==0
drop if alter==10 & NacioEa==0
drop if alter==12 & NacioNabai==0
drop if alter==11 & CCAA~=2


save ElectoralData.dta, replace
log close
